import {
  Body,
  Controller,
  DefaultValuePipe,
  ForbiddenException,
  Get,
  HttpException,
  HttpStatus,
  Param,
  ParseBoolPipe,
  ParseIntPipe,
  ParseUUIDPipe,
  Post,
  Query,
  SetMetadata,
  UseFilters,
  UseGuards,
  UseInterceptors,
  UsePipes,
} from '@nestjs/common';
import { CatsService } from './cats.service';
import { Cat } from 'src/modules/cats/interfaces/cat.interface';
import { HttpExceptionFilter } from './http-exception.filter';
import { ZodValidationPipe } from './zodvalidation.pipe';
import { CreateCatDto, createCatSchema } from './dto/cats.dto';
import { UserByIdPipe } from './user-transform.pipe';
import { RolesGuard } from './roles.guard';
import { Roles } from './roles.decorator';
import { LoggingInterceptor } from './logging.interceptor';
import { TransformInterceptor } from './transform.interceptor';

@Controller('cats')
@UseGuards(RolesGuard)
// @UseInterceptors(LoggingInterceptor)
// @UseFilters(new HttpExceptionFilter())
@UseInterceptors(TransformInterceptor)
export class CatsController {
  constructor(private catsSerivce: CatsService) {}

  // @Post()
  // @UseFilters(new HttpExceptionFilter())
  // async create(@Body() createCat:any){
  //     // throw new ForbiddenException();
  //     this.catsSerivce.create(createCat);
  // }

  @Post()
  @UsePipes(new ZodValidationPipe(createCatSchema))
  @Roles(['admin'])
  // @SetMetadata('roles',['admin'])
  async create(@Body() createCatDto: CreateCatDto) {
    // throw new ForbiddenException();
    this.catsSerivce.create(createCatDto);
  }

  @Get()
  async findAll(
    @Query('activeOnly', new DefaultValuePipe(false), ParseBoolPipe)
    activeOnly: boolean,
    @Query('page', new DefaultValuePipe(0), ParseIntPipe) page: number,
  ) {
    if (isNaN(page)) {
      throw new ForbiddenException();
    }
    try {
      return this.catsSerivce.findAll({ activeOnly, page });
    } catch (error) {
      throw new HttpException(
        {
          status: HttpStatus.FORBIDDEN,
          error: 'This is a custom message',
        },
        HttpStatus.FORBIDDEN,
        {
          cause: error,
        },
      );
    }
  }

  // @Get(":id")
  // async findOne(@Param('id', new ParseIntPipe({errorHttpStatusCode:HttpStatus.NOT_ACCEPTABLE})) id: number){
  //     return this.catsSerivce.findOne(id);
  // }

  // @Get(":id")
  // async findOne(@Query('id', ParseIntPipe) id: number){
  //     return this.catsSerivce.findOne(id);
  // }

  // @Get(":uuid")
  // async findOne(@Param('uuid', new ParseUUIDPipe()) id: string){
  //     return this.catsSerivce.findOne(id);
  // }

  @Get(':id')
  async findOne(@Param('id', UserByIdPipe) userEntiry: UserEntity) {
    return userEntiry;
  }
}
